package cn.mumzone.pos.model.source;

import com.alibaba.fastjson.JSON;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import cn.mumzone.basic.mvp.BaseRepository;
import cn.mumzone.basic.net.http.callback.ApiCallBack;
import cn.mumzone.basic.net.response.BaseResponse;
import cn.mumzone.basic.util.RxUtil;
import cn.mumzone.basic.util.StringUtils;
import cn.mumzone.pos.AppConstants;
import cn.mumzone.pos.api.ApiRequest;
import cn.mumzone.pos.model.CashServiceResult;
import cn.mumzone.pos.model.ReserveInfoEntity;
import cn.mumzone.pos.model.ReserveOrderEntity;
import cn.mumzone.pos.model.ReserveTimeEntity;
import cn.mumzone.pos.model.ReturnEntity;
import cn.mumzone.pos.model.ServerEntity;
import cn.mumzone.pos.model.ServerResultEntity;
import cn.mumzone.pos.model.ServerTimeTableEntity;
import cn.mumzone.pos.model.ServiceEntity;
import cn.mumzone.pos.model.ShopUsableInfoEntity;
import cn.mumzone.pos.util.AppUtil;
import okhttp3.MediaType;
import okhttp3.RequestBody;
import retrofit2.Response;

/**
 * Created by qiuzhenhuan on 2019/6/26.
 */

public class ReserveRepository implements BaseRepository {


    public void getShopUsableServiceList(String memberId, ApiCallBack<ShopUsableInfoEntity> callBack) {
        ApiRequest.getReserveService()
                .getShopUsableServiceList(memberId)
                .compose(RxUtil.<Response<BaseResponse<ShopUsableInfoEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    public void getShopUsableServerList(String memberId, ApiCallBack<ShopUsableInfoEntity> callBack) {
        ApiRequest.getReserveService()
                .getShopUsableServerList(memberId)
                .compose(RxUtil.<Response<BaseResponse<ShopUsableInfoEntity>>>applySchedulers())
                .subscribe(callBack);
    }

    /**
     * 添加产康/小儿游泳
     *
     * @param orderType       1:产康 2:小儿游泳
     * @param status          ReserveOrderEntity.ORDER_STATUS_ARRIVED : ReserveOrderEntity.ORDER_STATUS_RESERVED
     * @param appointmentTime
     * @param memberId
     * @param username
     * @param phone
     * @param serviceList
     * @param callBack
     */
    public void addReserve(String orderType, String status, String appointmentTime, String memberId, String username, String phone, List<ServiceEntity> serviceList, ApiCallBack<ReserveOrderEntity> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("orderType", orderType);
        requestMap.put("status", status);
        requestMap.put("appointmentTime", appointmentTime);
        requestMap.put("memberCompanyId", memberId);
        requestMap.put("billingUser", memberId);
        requestMap.put("userName", username);
        requestMap.put("userPhone", phone);
        requestMap.put("serviceList", JSON.toJSONString(serviceList));

        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .addReserve(body)
                .compose(RxUtil.<Response<BaseResponse<ReserveOrderEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    public void addServiceToReserve(ServiceEntity serviceEntity, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        ArrayList<ServiceEntity> serviceList = new ArrayList<>();
        serviceList.add(serviceEntity);
        requestMap.put("serviceList", JSON.toJSONString(serviceList));
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .addServiceToReserve(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }


    public void deleteSubService(String subAppointOrderId, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("subAppointOrderId", subAppointOrderId);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .deleteSubService(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void deleteServe(String appointmentId, ApiCallBack<String> callBack) {
        ApiRequest.getReserveService()
                .deleteServe(appointmentId)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void selectOrderAppointmentDetail(String appointmentId, String type, ApiCallBack<ReserveOrderEntity> callBack) {
        ApiRequest.getReserveService()
                .selectOrderAppointmentDetail(appointmentId, type)
                .compose(RxUtil.<Response<BaseResponse<ReserveOrderEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    public void updateReserveService(String subAppointOrderId, String technicianId, String technicianName, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("subAppointOrderId", subAppointOrderId);
        requestMap.put("technicianId", technicianId);
        requestMap.put("technicianName", technicianName);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .updateReserveService(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void updateAppointmentInfo(String appointmentId, String memberId, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("appointmentId", appointmentId);
        requestMap.put("billingUser", memberId);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .updateAppointmentInfo(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getUsableTechnicianList(String updateType, String appointmentId, String appointmentTime, List<ServiceEntity> serviceList, ApiCallBack<ServerResultEntity> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("appointmentId", StringUtils.getString(appointmentId));
        requestMap.put("appointmentTime", appointmentTime);
        requestMap.put("updateType", updateType);
        requestMap.put("orderType", AppUtil.hasServiceBusiness() ? ReserveOrderEntity.ORDER_TYPE_RESERVE
                : ReserveOrderEntity.ORDER_TYPE_BABYSWIM);
        requestMap.put("serviceList", JSON.toJSONString(serviceList));
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .getUsableTechnicianList(body)
                .compose(RxUtil.<Response<BaseResponse<ServerResultEntity>>>applySchedulers())
                .subscribe(callBack);
    }


    public void getNoOccupyServer(String appointmentId, String appointmentTime, ApiCallBack<List<ServerEntity>> callBack) {
        ApiRequest.getReserveService()
                .getNoOccupyServer(appointmentId, appointmentTime)
                .compose(RxUtil.<Response<BaseResponse<List<ServerEntity>>>>applySchedulers())
                .subscribe(callBack);
    }


    public void endOccupy(String appointmentId, String subAppointmentId, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("appointmentId", appointmentId);
        requestMap.put("id", subAppointmentId);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .endOccupy(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void endAllOccupy(String appointmentId, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("appointmentId", appointmentId);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .endAllOccupy(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getAppointmentList(String status, String keyWord, String orderCode, ApiCallBack<CashServiceResult> callBack) {
        ApiRequest.getReserveService()
                .selectAppointmentList(AppUtil.hasServiceBusiness() ? "0" : "1", status, keyWord, orderCode)
                .compose(RxUtil.<Response<BaseResponse<CashServiceResult>>>applySchedulers())
                .subscribe(callBack);
    }

    public void updateServeState(String appointmentId, String state, ApiCallBack<String> callBack) {
        ApiRequest.getReserveService()
                .updateServeState(appointmentId, state)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getUpdateTimeByOrderCode(String appointmentId, String date, ApiCallBack<List<ReserveTimeEntity>> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("appointmentId", appointmentId);
        requestMap.put("date", date);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .getUpdateTimeByOrderCode(body)
                .compose(RxUtil.<Response<BaseResponse<List<ReserveTimeEntity>>>>applySchedulers())
                .subscribe(callBack);
    }

    public void updateAppointmentTime(String appointmentId, String date, ApiCallBack<String> callBack) {
        HashMap<String, String> requestMap = new HashMap<>();
        requestMap.put("appointmentId", appointmentId);
        requestMap.put("date", date);
        RequestBody body = RequestBody.create(MediaType.parse("application/json; charset=utf-8"), JSON.toJSONString(requestMap));
        ApiRequest.getReserveService()
                .updateAppointmentTime(body)
                .compose(RxUtil.<Response<BaseResponse<String>>>applySchedulers())
                .subscribe(callBack);
    }

    /**
     * 获取小儿游泳可用服务&技师列表
     *
     * @param memberId 会员id 获取是否有次卡标记
     * @param callBack
     */
    public void getServicesAndServers(String memberId, ApiCallBack<ShopUsableInfoEntity> callBack) {
        ApiRequest.getReserveService()
                .getShopUsableServiceList(memberId)
                .compose(RxUtil.<Response<BaseResponse<ShopUsableInfoEntity>>>applySchedulers())
                .subscribe(callBack);
    }

    public void getServerTimeTable(String newDate, String orderType, ApiCallBack<ServerTimeTableEntity> callBack) {
        ApiRequest.getReserveService()
                .getServerTimeTable(newDate, orderType)
                .compose(RxUtil.<Response<BaseResponse<ServerTimeTableEntity>>>applySchedulers())
                .subscribe(callBack);
    }

}
